﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;

namespace LeHouse.Server.WebApi.Controllers
{
    public class CommunityController : ApiController
    {
        Collateral_CommunityService bll = new Collateral_CommunityService();
        /// <summary>
        /// 小区状况的信息添加
        /// </summary>
        /// <param name="s">ajax传过来的需要添加的数据</param>
        /// <returns></returns>       
        [HttpPost]
        [Route("api/Community/Postinsert")]
        public bool Insert([FromBody] Collateral_Community parms)
        {
            if (count(parms.T_ReportNumber) > 0)
            {
                Collateral_Community parms1 = new Collateral_Community();
            //   parms1.C_id = Convert.ToInt64(DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond);
                parms1.T_ReportNumber = parms.T_ReportNumber;
                parms1.T_Project = parms.T_Project;
                parms1.T_SourceID = parms.T_SourceID;
               // parms1. = parms.C_source;
                parms1.T_CloseID = parms.T_CloseID;
                parms1.T_BuildingID = parms.T_BuildingID;
                parms1.T_TrafficID = parms.T_TrafficID;
                parms1.T_AfforestID = parms.T_AfforestID;
                parms1.T_VolumeID = parms.T_VolumeID;
                parms1.T_StallID = parms.T_StallID;
                parms1.T_PropertyID = parms.T_PropertyID;
                parms1.T_Else = parms.T_Else;
              //  parms1 = 1;
                //var s=  Request["parms"];
                Collateral_CommunityService insert = new Collateral_CommunityService();
                if (insert.Update(parms1, t => t.T_ReportNumber == parms.T_ReportNumber))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                Collateral_Community parms1 = new Collateral_Community();
            //    parms1.C_id = Convert.ToInt64(DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond);
                parms1.T_ReportNumber = parms.T_ReportNumber;
                parms1.T_Project = parms.T_Project;
                parms1.T_SourceID = parms.T_SourceID;
                // parms1. = parms.C_source;
                parms1.T_CloseID = parms.T_CloseID;
                parms1.T_BuildingID = parms.T_BuildingID;
                parms1.T_TrafficID = parms.T_TrafficID;
                parms1.T_AfforestID = parms.T_AfforestID;
                parms1.T_VolumeID = parms.T_VolumeID;
                parms1.T_StallID = parms.T_StallID;
                parms1.T_PropertyID = parms.T_PropertyID;
                parms1.T_Else = parms.T_Else;
                //var s=  Request["parms"];
                Collateral_CommunityService insert = new Collateral_CommunityService();
                if (insert.Insert(parms1) == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }


        }

        /// <summary>
        /// 根据报告号获取小区状况的信息
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Community/SelectByid")]
        public Collateral_Community showcommunity([FromUri]string P_ReportNumber)
        {
            if (count(P_ReportNumber) > 0)
            {
                Collateral_FacilityService showcomm = new Collateral_FacilityService();
                Collateral_Community sss = bll.Community(P_ReportNumber);
                return sss;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 根据报告号去查询小区是否有该条记录
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>
        public int count(string P_ReportNumber)
        {
            return bll.count(P_ReportNumber);
        }

    }
}
